package com.xq.shop.controller;


import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.dto.RegisterDto;
import com.xq.shop.dto.StringDto;
import com.xq.shop.service.IUserService;
import com.xq.shop.service.IWebUserService;
import com.xq.shop.vo.UserVo;
import com.xq.shop.vo.WebUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import utils.BasePage;
import utils.BaseResult;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 网页用户表 前端控制器
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-18
 */
@RestController
@RequestMapping("/web/user")
public class WebUserController {

    private final IWebUserService webUserService;
    private final IUserService userService;

    @Autowired
    public WebUserController(IWebUserService webUserService, IUserService userService) {
        this.webUserService = webUserService;
        this.userService = userService;
    }

    @RequestMapping("/register")
    public BaseResult<Void> register(@RequestBody @Validated RegisterDto dto) {
        BaseResult<Void> result = new BaseResult<>();
        webUserService.register(dto);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/sendEmailCode")
    public BaseResult<Void> sendEmailCode(@RequestBody @Validated StringDto dto) {
        BaseResult<Void> result = new BaseResult<>();
        webUserService.sendEmailCode(dto);
        result.initializeSuccess();
        return result;
    }

    @RequestMapping("/page")
    public BaseResult<BasePage<WebUserVo>> page(@RequestBody QueryDto dto, HttpServletRequest request) {
        UserVo userVo = userService.getInfo(request);
        BaseResult<BasePage<WebUserVo>> result = new BaseResult<>();
        BasePage<WebUserVo> page = webUserService.pageUser(dto, userVo);
        result.initializeSuccess(page);
        return result;
    }

    @RequestMapping("/changeMark")
    public BaseResult<Void> page(@RequestBody @Validated IntegerDto dto, HttpServletRequest request) {
        userService.getInfo(request);
        BaseResult<Void> result = new BaseResult<>();
        webUserService.changeMark(dto);
        result.initializeSuccess();
        return result;
    }


}
